package com.zb.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.zb.entity.AccounLoginInfo;
import com.zb.entity.ZjAccount;
import com.zb.exception.SMSException;
import com.zb.secure.BCryptUtil;
import com.zb.service.ZjAccountService;
import com.zb.system.service.ConAccountService;
import com.zb.util.PCSmsUtil;
import com.zb.utils.VerificUtil;
import com.zb.vo.AccountPasswordVo;
import com.zb.vo.RegisterAccountVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @Author WEN
 * @Date 2024/3/24 0024
 **/
@Service
public class ConAccountServiceImpl implements ConAccountService {
    @Resource
    private ZjAccountService accountService;

    // 注册账号
    @Override
    public void register(RegisterAccountVo accountVo) throws SMSException {
        // 校验验证码
        PCSmsUtil.checkRegisterVerificCode(accountVo.getPhone(), accountVo.getCode());
        Assert.isFalse(accountService.existByPhone(accountVo.getPhone()), "该手机号已被注册！", accountVo.getPhone());
        ZjAccount account = new ZjAccount();
        account.setId(IdWorker.getId());
        account.setSalt(BCryptUtil.generatorSalt());
        account.setAccount("ZB_" + BCryptUtil.generatorSalt().substring(10, 20));
        account.setPhone(accountVo.getPhone());
        account.setPassword(BCryptUtil.encryPassword(accountVo.getPassword(), account.getSalt()));
        account.setName(accountVo.getName());
        account.setNickname(accountVo.getNickname());
        account.setAvatar("");
        account.setStatus(0);
        accountService.save(account);
    }

    // 未登录修改密码
    @Override
    public void notLoginUpdatePassword(AccountPasswordVo passwordVo) throws SMSException {
        Assert.isTrue(Objects.equals(1, passwordVo.getType()) || Objects.equals(2, passwordVo.getType()), "改密类型不合法", passwordVo.getType());
        // 验证码
        if (Objects.equals(1, passwordVo.getType())) {
            PCSmsUtil.checkUpdateVerificCode(passwordVo.getPhone(), passwordVo.getOldPassword());
        }
        ZjAccount account = accountService.getByPhone(passwordVo.getPhone());
        Assert.isTrue(null != account, "该手机号不存在，请检查手机号是否输入错误！", passwordVo.getPhone());
        // 旧密码
        if (Objects.equals(2, passwordVo.getType())) {
            // 校验密码
            Assert.isTrue(Objects.equals(account.getPassword(), BCryptUtil.encryPassword(passwordVo.getOldPassword(), account.getSalt())), "旧密码错误！", null);
        }

        ZjAccount ac = new ZjAccount();
        ac.setId(account.getId());
        ac.setSalt(BCryptUtil.generatorSalt());
        ac.setPassword(BCryptUtil.encryPassword(passwordVo.getNewPassword(), ac.getSalt()));
        accountService.updateById(ac);
    }

    // 修改密码
    @Override
    public void updatePassword(Long id, AccountPasswordVo passwordVo) throws SMSException {
        Assert.isTrue(Objects.equals(1, passwordVo.getType()) || Objects.equals(2, passwordVo.getType()), "改密类型不合法", passwordVo.getType());
        // 验证码
        if (Objects.equals(1, passwordVo.getType())) {
            PCSmsUtil.checkUpdateVerificCode(passwordVo.getPhone(), passwordVo.getOldPassword());
        }
        // 旧密码
        if (Objects.equals(2, passwordVo.getType())) {
            ZjAccount account = accountService.getById(id);
            Assert.isTrue(Objects.equals(account.getPassword(), BCryptUtil.encryPassword(passwordVo.getOldPassword(), account.getSalt())), "旧密码错误！", null);
        }
        ZjAccount account = new ZjAccount();
        account.setId(id);
        account.setSalt(BCryptUtil.generatorSalt());
        account.setPassword(BCryptUtil.encryPassword(passwordVo.getNewPassword(), account.getSalt()));
        accountService.updateById(account);
    }

    // 变更手机号
    @Override
    public void updatePhone(AccounLoginInfo info, String newPhone, String code) throws SMSException {
        Assert.isTrue(VerificUtil.phoneVerific(newPhone), "手机号格式错误！", newPhone);
        Assert.isTrue(StringUtils.isNotBlank(code), "验证码不能为空！", null);
        // 校验验证码
        PCSmsUtil.checkUpdateVerificCode(newPhone, code);
        // 校验手机号
        Assert.isFalse(accountService.existByPhone(newPhone), "该手机号已被使用，不可重复更换！", newPhone);

        ZjAccount account = new ZjAccount();
        account.setId(info.getId());
        account.setPhone(newPhone);
        accountService.updateById(account);
        info.setPhone(newPhone);
    }
}
